package com.example.tbplus14.utils.html;

import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;

public class HtmlToImg {




    public static void htmlToImg(String vcaType) throws Exception {
        HtmlParser htmlParser = new HtmlParserImpl();
        ClassPathResource classPathResource = new ClassPathResource(String.format("vCard%s.png", vcaType));
        String imagePath = classPathResource.getURL().getPath();

        ClassPathResource classPathResourceBg = new ClassPathResource("bodybg.png");
        String imageBgPath = classPathResourceBg.getURL().getPath();

        String HtmlTemplateStr = "<div class=\"all\" id=\"all\" style=\"display: flex;justify-content: center;\">\n" +
                "  <div style=\"width: 375px; background: url("+imageBgPath+") no-repeat; font-family: PingFangSC-Regular, PingFang SC; font-size: 12px;\" id=\"card\">\n" +
                "    <div class=\"anda\" style=\"margin-top: 14px; text-align: center;height: 20px;font-size: 14px;font-family: PingFangSC-Medium, PingFang SC;font-weight: 500;color: #FFFFFF;line-height: 20px;\">\n" +
                "      <span>Your Virtual Collection Account</span>\n" +
                "    </div>\n" +
                "    <div style=\"background-color: #ffffff;width: 93%;margin: 14px auto 0;border-radius:8px;padding-top: 14px;padding-bottom: 12px;\">\n" +
                "      <div class=\"card\" style=\"width: 91%;height: 182px;margin: 0 auto;background-size:100%;border-radius:4px\">\n" +
                "        <img id=\"pic\" src=\"\" alt=\"\" style=\"height: 182px\" />\n" +
                "      </div>\n" +
                "      <div class=\"tip\" style=\"width: 310px;height: 34px;font-size: 12px;font-family: PingFangSC-Regular, PingFang SC;font-weight: 400;color: #9b9898;line-height: 17px;margin: 14px auto 0;\">\n" +
                "        Your Virtual Collection Account has been opened and you can receive Instapay and Pesonet remittances through your Virtual Collection Account.\n" +
                "      </div>\n" +
                "      <div class=\"divisi\" style=\"width: 303px;height: 1px;background: #D8D8D8;margin: 21px auto 0;\"></div>\n" +
                "      <div class=\"Nomor\" style=\"display: flex;margin-left: 23px;margin-top: 8px;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Account number<span>:</span></span>\n" +
                "        <span id=\"NomorNum\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Name\" style=\"display:flex;height:auto;flex-direction:row;margin-left: 23px;margin-top: 3px;padding-right: 1px;align-items: flex-start;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Name<span>:</span></span>\n" +
                "        <span id=\"NameDetail\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Name\" style=\"display:flex;height:auto;flex-direction:row;margin-left: 23px;margin-top: 3px;padding-right: 1px;align-items: flex-start;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Identification Number <span>:</span></span>\n" +
                "        <span id=\"KartNum\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Name\" style=\"display:flex;height:auto;flex-direction:row;margin-left: 23px;margin-top: 3px;padding-right: 1px;align-items: flex-start;\">\n" +
                "        <span style=\"width: 144px;display: flex;justify-content: space-between;\">Registered bank <span>:</span></span>\n" +
                "        <span class=\"NameDetail\" style=\"width: 166px;word-break: break-all;float: right;margin-top: 4px;font-size: 12px;\" id=\"CabangNum\">&nbsp;&nbsp;</span>\n" +
                "      </div>\n" +
                "      <div class=\"Notice\" style=\"margin-top: 12px;font-size: 12px;color: #9b9898;  width: 97%;padding-left: 2%;\">\n" +
                "        Please check your Virtual Collection Account information. If you have any questions, please seek our help in time\n" +
                "      </div>\n" +
                "    </div>\n" +
                "    <span class=\"power\" style=\"width:100%;text-align: center;display:block;font-size: 12px;font-family: PingFangSC-Regular, PingFang SC;font-weight: 300;color: #e6c6c6;line-height: 20px;margin-top: 10px;margin-bottom: 10px;\">Power by Netbank</span>\n" +
                "  </div>\n" +
                "</div>";
        htmlParser.loadHtml(HtmlTemplateStr);

        ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
        imageRenderer.setWidth(390);
        imageRenderer.saveImage("C:\\Users\\tb14plus\\Desktop\\log\\aaa.png");

    }

    public static void main(String[] args) throws Exception{
        htmlToImg("NetBank");
//        imgToPdf();
    }

    private static void imgToPdf(){
        String imagePath = "C:\\Users\\tb14plus\\Desktop\\log\\aa.png";
        String pdfPath = "C:\\Users\\tb14plus\\Desktop\\log\\aa.pdf";
        try (PDDocument document = new PDDocument()) {
            // 创建新页面
            PDPage page = new PDPage();
            document.addPage(page);
            // 加载图片
            PDImageXObject image = PDImageXObject.createFromFile(imagePath, document);

            float width = image.getWidth();
            float height = image.getHeight();
            float maxWidth = 500;// 设置最大宽度
            float maxHeight= 500;// 设置最大高度
            float scale = Math.min(maxWidth / width, maxHeight / height);// 计算缩放比例
            float scaledwidth=width* scale;// 计算缩放后的宽度
            float scaledHeight=height*scale;// 计算缩放后的高度

            try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
                // 将图片添加到页面
                contentStream.drawImage(image,60,260, scaledwidth, scaledHeight);
            }
            // 保存 PDF 文件
            File file = new File(pdfPath);
            document.save(file);

            // oss上传


            if (file.exists()){
                System.out.println(file.getPath());
//                boolean delete = file.delete();
            }

            System.out.println("图片已成功转换为 PDF。");
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

}
